(function(){
    "use strict";
    YuanMap.DataProvider = {
        Geocoder: {
            searchOption: {
                extensions: "all"
            },
            /**
             * 根据给定的地址描述进行解析，支持中文、拼音
             * @param {String} address 
             * @param {GeocoderOptions} searchOption
             * @returns {Promise}
             */
            getLngLat: function(address, searchOption){
                var that = this;
                var defer = $.Deferred();
                var searchOption = $.extend({}, that.searchOption, searchOption);
                
                mapObj.plugin(["AMap.Geocoder"], function() {
                    var geoCoder = new AMap.Geocoder(searchOption);
                    AMap.event.addListener(geoCoder, "complete", function(result){
                        defer.resolve(result);
                    });   
                    AMap.event.addListener(geoCoder, "error", function(result){
                        defer.reject(result);
                    }); 
                    geoCoder.getLocation(address);
                });
                
                return defer.promise();   
            },
            /**
             * 根据给定坐标进行解析
             * @param {LngLat} lnglat
             * @param {GeocoderOptions} searchOption
             * @returns {Promise}
             */
            getAddress: function(lnglat, searchOption){
                var that = this;
                var defer = $.Deferred();
                var searchOption = $.extend({}, that.searchOption, searchOption);
                
                mapObj.plugin(["AMap.Geocoder"], function() {
                    var geoCoder = new AMap.Geocoder(searchOption);
                    AMap.event.addListener(geoCoder, "complete", function(result){
                        defer.resolve(result);
                    });   
                    AMap.event.addListener(geoCoder, "error", function(result){
                        defer.reject(result);
                    }); 
                    geoCoder.getAddress(lnglat);   
                });
                
                return defer.promise();
            }
        },
        Direction: {
            Transit: {
                /**
                 * 公交换乘
                 * @param {Array} lngLatArr LngLat组成的数组
                 * @param {String} city 城市名字，可以为汉字或者区号，如 010
                 * @param {BusSearchOption} searchOption 可选
                 * @returns {Promise} jQuery Promise
                 */
                byTwoLngLat: function(lngLatArr, city, searchOption) {
                    var defer = $.Deferred();
                    var transitSearch = searchOption ? new AMap.BusSearch(searchOption) : new AMap.BusSearch();
                    transitSearch.byTwoPoi(lngLatArr, city,function(result){
                        defer.resolve(result);
                    });
                    return defer.promise();
                }
            },
            Driving: {
                /**
                 * 根据起点和终点坐标，实现驾车路线规划
                 * @param {LngLat} origin 起点
                 * @param {LngLat} destination 终点
                 * @param {DrivingOptions} searchOption 可选
                 * @returns {Promise} jQuery Promise
                 */
                byTwoLngLat: function(origin, destination, searchOption) {
                    var defer = $.Deferred();
                    
                    mapObj.plugin(["AMap.Driving"], function() {
                        var drivingSearch = searchOption ? new AMap.Driving(searchOption) : new AMap.Driving();
                        AMap.event.addListener(drivingSearch, "complete", function(result){
                            defer.resolve(result);
                        });
                        AMap.event.addListener(drivingSearch, "error", function(result){
                            defer.reject(result);
                        });
                        drivingSearch.search(origin, destination);
                    });
                    
                    return defer.promise();
                },
                /**
                 * @ignore TODO
                 * 支持途径点的驾车导航
                 * @param {Array} lngLatArr 导航的起点、途经点、终点的经纬度坐标数组,即LngLat坐标数组
                 * @param {DrivingPolicy} policy 驾车策略
                 * @returns {Promise} jQuery Promise
                 */
                withWaypoints: function(lngLatArr, policy) {
                    var defer = $.Deferred();
                    //console.log(AMap.DrivingPolicy);debugger;
                    //var policy = policy || AMap.DrivingPolicy.LEAST_FEE;
                    mapObj.plugin(['AMap.DragRoute'],function(){
                        var dragRoute = new AMap.DragRoute(mapObj, lngLatArr);  
                        dragRoute.search();
                        var routes = dragRoute.getRoute();
                        defer.resolve(routes);
                    });
                    return defer.promise();
                }
            },
            Walking: {
                /**
                 * 步行导航
                 * @param {LngLat} origin 起点
                 * @param {LngLat} destination 终点
                 * @returns {Promise} jQuery Promise
                 */
                byTwoLngLat: function(origin, destination) {
                    var defer = $.Deferred();
                    mapObj.plugin(["AMap.Walking"], function() {
                        var walkingSearch = new AMap.Walking();
                        AMap.event.addListener(walkingSearch, "complete", function(result){
                            defer.resolve(result);
                        });
                        AMap.event.addListener(walkingSearch, "error", function(result){
                            defer.reject(result);
                        });
                        walkingSearch.search(origin, destination);
                    });
                    return defer.promise();
                }
            }
        },
        Place: {
            /**
             * 根据关键字搜索 关键字支持中文|中文全拼、繁体、英文
             * @param {String} keyword 关键字
             * @param {PlaceSearchOptions} searchOption 对象初始化时的参数选项
             * @returns {Promise} jQuery Promise
             */
            search: function(keyword, searchOption) {
                var defer = $.Deferred();
                mapObj.plugin(["AMap.PlaceSearch"], function() {
                    var placeSearch = searchOption ? new AMap.PlaceSearch(searchOption) : new AMap.PlaceSearch();
                    AMap.event.addListener(placeSearch, "complete", function(result){
                        defer.resolve(result);
                    });
                    AMap.event.addListener(placeSearch, "error", function(result){
                        defer.reject(result);
                    });
                    placeSearch.search(keyword);
                });
                return defer.promise();
            },
            /**
             * 根据PGUID 查询POI 详细信息
             * @param {String} PGUID PGUID全局唯一ID
             * @param {PlaceSearchOptions} searchOption 对象初始化时的参数选项
             * @returns {Promise} jQuery Promise
             */
            getDetails: function(PGUID, searchOption) {
                var defer = $.Deferred();
                mapObj.plugin(["AMap.PlaceSearch"], function() {
                    var placeSearch = searchOption ? new AMap.PlaceSearch(searchOption) : new AMap.PlaceSearch();
                    AMap.event.addListener(placeSearch, "complete", function(result){
                        defer.resolve(result);
                    });
                    AMap.event.addListener(placeSearch, "error", function(result){
                        defer.reject(result);
                    });
                    placeSearch.getDetails(PGUID);
                });
                return defer.promise();
            },
            /**
             * 根据中心点经纬度、半径以及关键字进行周边查询
             * @param {String} keyword 关键字
             * @param {LngLat} centerLngLat 中心点坐标
             * @param {Number} radius 半径
             * @param {PlaceSearchOptions} searchOption 对象初始化时的参数选项
             * @returns {Promise} jQuery Promise
             */
            nearbySearch: function(keyword, centerLngLat, radius, searchOption) {
                var defer = $.Deferred();
                mapObj.plugin(["AMap.PlaceSearch"], function() {
                    var placeSearch = searchOption ? new AMap.PlaceSearch(searchOption) : new AMap.PlaceSearch();
                    AMap.event.addListener(placeSearch, "complete", function(result){
                        defer.resolve(result);
                    });
                    AMap.event.addListener(placeSearch, "error", function(result){
                        defer.reject(result);
                    });
                    placeSearch.searchNearBy(keyword, centerLngLat, radius);
                });
                return defer.promise();
            }
        },
        Autocomplete: {
            /**
             * 根据输入关键字提示匹配信息，支持中文、拼音
             * @param {string} keyword 输入关键字
             * @param {AutocompleteOptions} searchOption 对象初始化时的参数选项
             * @returns {Promise} jQuery Promise
             */
            getPlace: function(keyword, searchOption) {
                var defer = $.Deferred();
                mapObj.plugin(["AMap. Autocomplete"], function() {
                    var service = searchOption ? new AMap.Autocomplete(searchOption) : new AMap. Autocomplete();
                    AMap.event.addListener(service, "complete", function(result){
                        defer.resolve(result);
                    });  
                    AMap.event.addListener(service, "error", function(result){
                        defer.reject(result);
                    });
                    service.search(keyword);  
                });
                return defer.promise();
            }
        },
        CitySearch: {
            /**
             * 自动获取用户IP，返回当前用户所在城市
             * @returns {Promise} jQuery Promise
             */
            getLocalCity: function() {
                var defer = $.Deferred();
                mapObj.plugin(["AMap.CitySearch"], function() {
                    var citysearch = new AMap.CitySearch();
                    AMap.event.addListener(citysearch, "complete", function(result){
                        defer.resolve(result);
                    });
                    AMap.event.addListener(citysearch, "error", function(result){
                        defer.reject(result);
                    });
                    citysearch.getLocalCity();  
                });  
                return defer.promise();
            },
            /**
             * 
             * @param {String} IP 输入IP
             * @returns {Promise} jQuery Promise
             */
            getCityByIp: function(IP) {
                var defer = $.Deferred();
                mapObj.plugin(["AMap.CitySearch"], function() {
                    var citysearch = new AMap.CitySearch();
                    AMap.event.addListener(citysearch, "complete", function(result){
                        defer.resolve(result);
                    });
                    AMap.event.addListener(citysearch, "error", function(result){
                        defer.reject(result);
                    });
                    citysearch.getCityByIp(IP);  
                });  
                return defer.promise();
            }
        },
        Partition: {
            /**
             * 得到所有省份信息。
             * @returns {Promise} jQuery Promise
             */
            byProvince: function(){
                var defer = $.Deferred();
                var partitionSearch = new AMap.Partition();
                partitionSearch.byProvince( function(result){
                    defer.resolve(result);
                });
                return defer.promise();
            },
            /**
             * 根据城市名称，查询城市信息。
             * @param {String} keyword 城市编码
             * @returns {Promise} jQuery Promise
             */
            byCity: function(keyword){
                var defer = $.Deferred();
                var partitionSearch = new AMap.Partition();
                partitionSearch.byCity(keyword, function(result){
                    defer.resolve(result);
                });
                return defer.promise();
            },
            /**
             * 根据城市名称与区县名称查询区县信息。
             * @param {String} keyword 区县名称
             * @param {String} city 城市编码
             * @returns {Promise} jQuery Promise
             */
            byDistrict: function(keyword, city) {
                var defer = $.Deferred();
                var partitionSearch = new AMap.Partition();
                partitionSearch.byDistrict(keyword, city, function(result){
                    defer.resolve(result);
                });
                return defer.promise();
            }
        },
        TransitLine: {
            /**
             * 根据ID 或ID 数组查看公交线路信息。
             * @param {String/Array} keyword 公交线路ID 或ID 数组
             * @param {String} city 城市编码
             * @param {BusSearchOption} searchOption 公交导航查询对象初始化时的参数选项
             * @returns {Promise} jQuery Promise
             */
            byBusId: function(keyword, city, searchOption){
                var defer = $.Deferred();
                var busSearch = searchOption ? new AMap.BusSearch(searchOption) : new AMap.BusSearch();
                busSearch.byBusId(keyword, city,function(result){  
                    defer.resolve(result);
                });  
                return defer.promise();
            },
            /**
             * 
             * @param {String} keyword 公交线路名称
             * @param {String} city 城市编码
             * @param {BusSearchOption} searchOption 公交导航查询对象初始化时的参数选项
             * @returns {Promise} jQuery Promise
             */
            byBusName: function(keyword, city, searchOption){
                var defer = $.Deferred();
                var busSearch = searchOption ? new AMap.BusSearch(searchOption) : new AMap.BusSearch();
                busSearch.byBusName(keyword, city,function(result){  
                    defer.resolve(result);
                });  
                return defer.promise();
            },
            /**
             * 根据站点名称查询经过该站点的所有公交线路信息
             * @param {String} keyword 公交站点名称
             * @param {String} city 城市编码
             * @param {BusSearchOption} searchOption 公交导航查询对象初始化时的参数选项
             * @returns {Promise} jQuery Promise
             */
            byBusStation: function(keyword, city, searchOption) {
                var defer = $.Deferred();
                var busSearch = searchOption ? new AMap.BusSearch(searchOption) : new AMap.BusSearch();
                busSearch.byBusStation(keyword, city,function(result){  
                    defer.resolve(result);
                });  
                return defer.promise();
            }
        },
        RoadSearch: {
            /**
             * 根据道路名称查询道路详细信息。
             * @param {String} roadName 道路名称
             * @param {String} city 要查询的道路所在的城市，城市名称或区号
             * @param {RoadSearchOption} searchOption 道路查询对象初始化时的参数选项
             * @returns {Promise} jQuery Promise
             */
            byRoadName: function (roadName, city, searchOption){
                var defer = $.Deferred();
                var roadSearch = searchOption ? new AMap.RoadSearch(searchOption) : new AMap.RoadSearch();
                roadSearch.roadInfoSearchByRoadName(roadName, city, function(result){
                    defer.resolve(result);
                });
                return defer.promise();
            }
        }
    };
})();